package com.bbc.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bbc.R;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/9/26.
 */

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.Vholder> {
    private Context mContext;
    private List<String> mList;

    public HomeAdapter(Context context, List<String> mList) {
        this.mContext = context;
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void initDatas(List<String> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void addData(List<String> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public Vholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_fragment, parent, false);
        return new Vholder(view);
    }

    @Override
    public void onBindViewHolder(Vholder holder, int position) {
        if (mList != null && mList.size() > 0) {
            String tvName = mList.get(position);
            holder.mTvTestName.setText(tvName);
            Glide.with(mContext).load("http://xianmei.oss-cn-shenzhen.aliyuncs.com/xianmeiclub/2017/09/12/1505197679458.jpg")
                    .error(R.drawable.home_on)
                    .into(holder.mIvTestImage);
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    public class Vholder extends RecyclerView.ViewHolder {
        private TextView mTvTestName;
        private ImageView mIvTestImage;

        public Vholder(View itemView) {
            super(itemView);
            mTvTestName = itemView.findViewById(R.id.tv_test_name);
            mIvTestImage = itemView.findViewById(R.id.iv_test_image);
        }
    }
}
